﻿@{
    Layout = "~/Views/Shared/_Index.cshtml";
}
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment HostingEnvironment
@section header{
    @BundlerHelper.Render(HostingEnvironment.ContentRootPath, Url.Content("~/lib/zTree/v3/css/metroStyle/metroStyle.min.css"))
    @BundlerHelper.Render(HostingEnvironment.ContentRootPath, Url.Content("~/lib/zTree/v3/js/ztree.min.js"))

    @BundlerHelper.Render(HostingEnvironment.ContentRootPath, Url.Content("~/lib/jquery.layout/1.4.4/jquery.layout-latest.min.css"))
    @BundlerHelper.Render(HostingEnvironment.ContentRootPath, Url.Content("~/lib/jquery.layout/1.4.4/jquery.layout-latest.min.js"))
}


<div class="container-div">
    <div class="row">
        <div id="searchDiv" class="col-sm-12 search-collapse">
            <input type="hidden" id="shopid" col="shopid">
            <div class="select-list">
                <ul>
                    <li>
                        商铺名称：<input id="shopname" col="shopname" type="text" />
                    </li>
                    <li>
                        商品名称：<input id="goodsname" col="goodsname" type="text" />
                    </li>
                    <li>
                        <a id="btnSearch" class="btn btn-primary btn-sm" onclick="searchGrid()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                    </li>
                </ul>
            </div>
        </div>
        <div class="btn-group-sm hidden-xs" id="toolbar">
            <a id="btnAdd" class="btn btn-success" onclick="showSaveForm(true)"><i class="fa fa-plus"></i> 新增</a>
            <a id="btnEdit" class="btn btn-primary disabled" onclick="showSaveForm(false)"><i class="fa fa-edit"></i> 修改</a>
            <a id="btnDelete" class="btn btn-danger disabled" onclick="deleteForm()"><i class="fa fa-remove"></i> 删除</a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="gridTable" data-mobile-responsive="true"></table>

        </div>
    </div>
</div>

<script type="text/javascript">

    var shopid = ys.request("shopid");

    $(function () {

        $("#shopid").val(shopid);

        initGrid();
    });

    function initGrid() {
        var queryUrl = '@Url.Content("~/QixingManage/StoreGoods/GetPageListJson")';
        $('#gridTable').ysTable({
            url: queryUrl,
            columns: [
                { checkbox: true, visible: true },
                { field: 'Id', title: 'Id', visible: false },
                { field: 'shop_name', title: '商铺名称' },
                { field: 'goods_core', title: '商品编号' },
                { field: 'goods_name', title: '商品名称' },
                {
                    field: 'goods_image', title: '商品封面', formatter: function (value, row, index) {
                        if (index % 2 == 0) {
                            return '<img class="img-circle img-xs" src="' + row.goods_image + '" onclick=showImage("' + row.goods_image + '") />';
                        }
                        else {
                            return '<img class="img-circle img-xs" src="' + row.goods_image + '" onclick=showImage("' + row.goods_image + '") />';
                        }
                    }
                },
                {
                    field: 'is_master', title: '是否推荐', formatter: function (value, row, index) {
                    if (row.is_master == "@GoodsIsMasterStatusEnum.Yes.ParseToInt()") {
                            return '<span class="badge badge-primary">' + "@GoodsIsMasterStatusEnum.Yes.GetDescription()" + '</span>';
                        } else {
                            return '<span class="badge badge-warning">' + "@GoodsIsMasterStatusEnum.No.GetDescription()" + '</span>';
                        }
                    }
                },
                {
                    field: 'publish_status', title: '是否上架', formatter: function (value, row, index) {
                     if (row.publish_status == "@GoodsPublishStatusEnum.Yes.ParseToInt()") {
                            return '<span class="badge badge-primary">' + "@GoodsPublishStatusEnum.Yes.GetDescription()" + '</span>';
                        } else {
                            return '<span class="badge badge-warning">' + "@GoodsPublishStatusEnum.No.GetDescription()" + '</span>';
                       }
                    }
                },
                //{
                //    field: 'publish_status', title: '是否上架', formatter:typeSwitch //引入开关
                //},
                {
                    field: 'indatetime', title: '录入时间', formatter: function (value, row, index) {
                        return ys.formatDate(value, "yyyy-MM-dd HH:mm:ss");
                    }
                },
                //{
                //    field: 'description', title: '备注'
                //},
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-info btn-xs" href="#" onclick="GoodsAttrList(\'' + row.Id + '\',\'' + row.goods_name + '\',\'' + row.goods_model_id +'\')"><i class="fa fa-list-ul"></i>货物属性</a>');
                        actions.push('<a class="btn btn-info btn-xs" href="#" onclick="WarehouseList(\'' + row.Id + '\',\'' + row.goods_name + '\',\''+row.goods_model_id+'\')"><i class="fa fa-list-ul"></i>货物管理</a>');
                        return actions.join('');
                    }
                }
            ],
            queryParams: function (params) {
                var pagination = $('#gridTable').ysTable('getPagination', params);
                var queryString = $('#searchDiv').getWebControls(pagination);
                return queryString;
            },
            onLoadSuccess: function () {
                //$("[name='switch']").bootstrapSwitch({
                //    onText: "启用",      // 设置ON文本
                //    offText: "禁用",    // 设置OFF文本
                //    onColor: "success",// 设置ON文本颜色(info/success/warning/danger/primary)
                //    offColor: "warning",  // 设置OFF文本颜色 (info/success/warning/danger/primary)
                //    size: "small",    // 设置控件大小,从小到大  (mini/small/normal/large)
                //    // 当开关状态改变时触发
                //    onSwitchChange: function (event, state) {
                //        id = this.value;
                //        $.post('student/update', { id: id, isUsed: state }, function () {

                //        });
                //    }
                //});
            },
        });
    }

    //表格中开关控件
    //function typeSwitch(value, row, index) {
    //    if (value) {
    //        return "<input value=" + row.id + " name='switch' type='checkbox' checked />";
    //    } else {
    //        return "<input value=" + row.id + " name='switch' type='checkbox' />";
    //    }
    //}

    function GoodsAttrList(id, name, model_id)
    {
        if (ys.isNullOrEmpty(model_id) || model_id == "null") {
            ys.msgError("请绑定商品型号!");
            return false;
        }
        var url = '@Url.Content("~/QixingManage/StoreGoodsAttr/StoreGoodsAttrIndex")' + '?goodsid=' + id + '&goodsname=' + name;
        createMenuItem(url, "商品属性管理");
     }

    function WarehouseList(id,name,model_id)
    {
        if (ys.isNullOrEmpty(model_id)||model_id=="null") {
            ys.msgError("请绑定商品型号!");
            return false;
        }
         var url = '@Url.Content("~/QixingManage/StoreGoodsProduct/StoreGoodsProductIndex")' + '?goodsid=' + id + '&goodsname=' + name;
        createMenuItem(url, "商品管理");
    }

    function showImage(imageUrl) {
        var html = '<img src="' + imageUrl + '" width="100%" height="100%" class="img-responsive" />';
        ys.openDialogContent({
            content: html,
            width: 'auto',
            height: 'auto',
            closeBtn: true
        });
    }

    function searchGrid() {
        $('#gridTable').ysTable('search');
        resetToolbarStatus();
    }

    function searchPageGrid() {
        $('#gridTable').ysTable('searchPage');
        resetToolbarStatus();
    }

    function showSaveForm(bAdd) {
        var id = 0;
        if (!bAdd) {
            var selectedRow = $('#gridTable').bootstrapTable('getSelections');
            if (!ys.checkRowEdit(selectedRow)) {
                return;
            }
            else {
                id = selectedRow[0].Id;
            }
        }
        ys.openDialog({
            title: id > 0 ? '商品编辑' : '商品添加',
            content: '@Url.Content("~/QixingManage/StoreGoods/StoreGoodsForm")' + '?id=' + id + '&shopid=' + shopid,
            width: '1200px',
            height: '800px',
            callback: function (index, layero) {
                var iframeWin = window[layero.find('iframe')[0]['name']];
                iframeWin.saveForm(index);
            }
        });
    }

    function deleteForm() {
        var selectedRow = $('#gridTable').bootstrapTable('getSelections');
        if (ys.checkRowDelete(selectedRow)) {
            ys.confirm('确认要删除选中的' + selectedRow.length + '条数据吗？', function () {
                var ids = ys.getIds(selectedRow);
                ys.ajax({
                    url: '@Url.Content("~/QixingManage/StoreGoods/DeleteFormJson")' + '?ids=' + ids,
                    type: 'post',
                    success: function (obj) {
                        if (obj.Tag == 1) {
                            ys.msgSuccess(obj.Message);
                            searchGrid();
                        }
                        else {
                            ys.msgError(obj.Message);
                        }
                    }
                });
            });
        }
    }
</script>
